package com.kay.car.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kay.car.entity.CarTable;
import com.kay.car.entity.PriceTable;
import com.kay.car.service.CarService;
import com.kay.car.service.DogService;
import com.kay.car.service.PriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class IndexController {
    @Autowired
    private CarService carService;
    @Autowired
    private DogService dogService;
    @Autowired
    private PriceService priceService;

    @RequestMapping("/")
    public String index(){
        return "index";
    }

    @RequestMapping("/update")
    @ResponseBody
    public CarTable update(@RequestBody CarTable car){
        return carService.update(car);
    }

    @RequestMapping("/updateUrl")
    @ResponseBody
    public String updateUrl(@RequestParam Long id,@RequestParam String url){
        return carService.updateUrl(id,url);
    }

    @RequestMapping("/setDog")
    @ResponseBody
    public String setDog(@RequestParam Long id,@RequestParam int dog){
        return carService.setDog(id,dog);
    }

    @RequestMapping("/getPrice")
    @ResponseBody
    public JSONObject getPrice(@RequestParam String key, @RequestParam String year){
        return carService.getPrice(key, year);
    }

    @RequestMapping("/getPriceAll")
    @ResponseBody
    public List<PriceTable> getPriceAll(@RequestParam(defaultValue = "") String key){
        return priceService.getPriceAll(key);
    }

    @RequestMapping("/getRecommend")
    @ResponseBody
    public JSONArray getRecommend(@RequestParam(defaultValue = "") String key){
        return carService.getRecommend(key);
    }

    @RequestMapping("/enable")
    @ResponseBody
    public String enable(@RequestParam(defaultValue = "") Long id){
        return carService.enable(id);
    }

    @RequestMapping("/getALLDog")
    @ResponseBody
    public List<String> getALLDog(){
        return dogService.findAll();
    }

    @RequestMapping("/addDog")
    @ResponseBody
    public String addDog(@RequestParam String name){
        return dogService.addDog(name);
    }

    @RequestMapping("/spider")
    @ResponseBody
    public String spider(){
        priceService.spider();
        return "ok";
    }
}
